package com.hoongs.v1.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoongs.v1.dto.ProductPageDto;
import com.hoongs.v1.entity.Cart;
import com.hoongs.v1.entity.User;
import com.hoongs.v1.mapper.CartMapper;
import com.hoongs.v1.mapper.ProductMapper;
import com.hoongs.v1.mapper.UserMapper;
import com.hoongs.v1.service.ProductService;
import com.hoongs.v1.utils.PResult;
import com.hoongs.v1.utils.Result;
import com.hoongs.v1.entity.Product;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ProductServiceImpl implements ProductService {

    public static final String NOT_FOUND_ARTICLE = "没有找到该商品！";

    @Resource
    private ProductMapper productMapper;
    @Resource
    private CartMapper cartMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public Result add(Product product) {
        if (productMapper.insert(product) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result remove(String id) {
        if (!findArticleById(id)) return Result.opError(NOT_FOUND_ARTICLE);
        if (productMapper.deleteById(id) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result update(Product product) {
        if (!findArticleById(product.getId())) return Result.opError(NOT_FOUND_ARTICLE);
        if (productMapper.updateById(product) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result selectById(String id) {
        Product product = productMapper.selectById(id);
        if (product == null) return Result.error(NOT_FOUND_ARTICLE);
        return Result.success(product);
    }

    @Override
    public Result selectListByUser(String name, String cate, String status, String token) {

        // 根据用户获取购物车数据
        User user = UserServiceImpl.getUserInfoByToken(token, userMapper);
        if (user == null) return Result.opError("请先登录！");
        List<Cart> cartlist = cartMapper.selectList(new QueryWrapper<Cart>().eq("user_id", user.getId()));

        QueryWrapper<Product> wrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        if (StringUtils.isNotBlank(cate)) {
            wrapper.like("cate", cate);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        Collection<Product> products = productMapper.selectList(wrapper);

        Collection<Object> results = null;
        for (Product productItem : products) {
            for (Cart cartItem : cartlist) {
                if (cartItem.getProductId().equals(productItem.getId())) {
                    Map stringObjectMap = BeanUtil.beanToMap(productItem);
                    stringObjectMap.put("number", cartItem.getNumber() == null ? 0 : cartItem.getNumber());
                    results.add(stringObjectMap);
                }
            }
        }

        return Result.success(results);
    }

    @Override
    public Result selectList(String name, String cate, String status) {
        QueryWrapper<Product> wrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        if (StringUtils.isNotBlank(cate)) {
            wrapper.like("cate_name", cate);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        Collection<Product> list = productMapper.selectList(wrapper);
        return Result.success(list);
    }

    @Override
    public Result selectPageList(ProductPageDto productDto) {
        if (!PResult.checkPageQuery(productDto.getPage(), productDto.getSize())) return Result.error("分页参数错误");

        Page<Product> daoPage = new Page<>(productDto.getPage(), productDto.getSize());
        QueryWrapper<Product> wrapper = new QueryWrapper<Product>();
        if (StringUtils.isNotBlank(productDto.getName())) {
            wrapper.like("name", productDto.getName());
        }
        if (StringUtils.isNotBlank(productDto.getCateName())) {
            wrapper.like("cate_name", productDto.getCateName());
        }
        if (StringUtils.isNotBlank(productDto.getCateId())) {
            wrapper.eq("cate_id", productDto.getCateId());
        }
        if (StringUtils.isNotBlank(productDto.getStatus())) {
            wrapper.eq("status", productDto.getStatus());
        }
        if (StringUtils.isNotBlank(productDto.getType())) {
            wrapper.like("type", productDto.getType());
        }
        IPage<Product> productDaoIPage = productMapper.selectPage(daoPage, wrapper.orderByDesc("create_time"));
        PResult rs = new PResult(productDaoIPage);
        return Result.success(rs);
    }

    /**
     * 根据Id查询文章是否存在
     */
    public Boolean findArticleById(String id) {
        Product product = productMapper.selectById(id);
        return product != null;
    }

}
